<?php
class Rcp_LpImport_CalendarImport {
	
	private $mapCalendars;
	private $mapDays;

	private function getYears() {
		$currentYear = (int)date('Y');
		$years = array();
		$years[] = $currentYear - 1;
		$years[] = $currentYear;
		$years[] = $currentYear + 1;
		return implode(', ', $years);
	}
	
	/**
	 * Metoda importuje dane kalendarza dla szablonu 'GLOWNY'.
	 * Metoda jest napisana w taki sposób, że gdy zechcemy aby wicej szablonów
	 * zostao importowane wystarczy zmodyfikować zapytanie w taki sposób aby
	 * pobierało dane dla wikszej liczby szablonów
	 * @throws Exception
	 */
	public function start() {
		$con = Propel::getConnection('vw_lp');
		$sql = "SELECT Szablon as template, Data as date, IloscGodzinNumeric as work_hours, DzienTyp as type
						FROM dbo.lp_vv_SzablonyKalendarzyDniDataIloscGodzin
						WHERE Szablon = 'GLOWNY' AND YEAR(Data) IN (" . $this->getYears() . ")
						ORDER BY Data DESC";
		$stmt = $con->prepare($sql);
		$stmt->execute();
		$lpGroupedDays = $this->getGroupedDays($stmt->fetchAll(PDO::FETCH_ASSOC), 'template');
		$this->mapCalendats = RcpCalendarsQuery::create()->find()->getArrayCopy('name');
		$this->mapDays = $this->getMapDays(RcpCalendarDaysQuery::create()->find());
		$con = Propel::getConnection('vgp_rcp');
		$con->beginTransaction();
		try {
			foreach ($lpGroupedDays as $groupName => $lpGroup) {
				$calendar = $this->getCalendar($groupName, $con);
				$calendar->save($con);
				foreach ($lpGroup as $lpDay) {
					$calendarDay = $this->getCalendarDay($lpDay['date'], $calendar, $con);
					$calendarDay->setType($lpDay['type']);
					$calendarDay->setWorkHours($lpDay['work_hours'] * 60);
					$calendarDay->save($con);
				}
			}
			$con->commit();
		} catch (Exception $e) {
			$con->rollBack();
			throw $e;
		}
	}
	
	/**
	 * @param string $groupName
	 * @param PDO A database connection $con
	 * @return RcpCalendars
	 */
	private function getCalendar($groupName) {
		if (!key_exists($groupName, $this->mapCalendats)) {
			$this->mapCalendats[$groupName] = new RcpCalendars();
			$calendar = $this->mapCalendats[$groupName];
			$calendar->setName($groupName);
		}
		else {
			$calendar = $this->mapCalendats[$groupName];
		}
		return $calendar;
	}
	
	/**
	 * @param string $date
	 * @param RcpCalendars $calendar
	 * @return RcpCalendarDays
	 */
	private function getCalendarDay($date, RcpCalendars $calendar) {
		$date = date_format(date_create($date), 'Y-m-d');
		if (!key_exists($calendar->getId(), $this->mapDays) || !key_exists($date, $this->mapDays[$calendar->getId()])) {
			$this->mapDays[$calendar->getId()][$date] = new RcpCalendarDays();
			$day = $this->mapDays[$calendar->getId()][$date];
			$day->setCalendars($calendar);
			$day->setDay($date);
		}
		else {
			$day = $this->mapDays[$calendar->getId()][$date];
		}
		return $day;
	}
	
	private function getMapDays($days) {
		$ret = array();
		foreach ($days as $day) {
			$ret[$day->getCalendarId()][$day->getDay('Y-m-d')] = $day;
		}
		return $ret;
	}
	
	private function getGroupedDays($days, $groupKey) {
		$groups = array();
		foreach ($days as $day) {
			$key = $day[$groupKey];
			$groups[$key][] = $day;
		}
		return $groups;
	}
	
}